shell script2 Min Read

Curl - Your friend for Rest APIs/Calls - Basic Commands

Gorav Singal

May 09, 2018

TL;DR

Use curl for GET, POST, PUT, and DELETE REST API calls from the command line, with options for headers, authentication, JSON payloads, and file downloads.

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally open any web URL through it, and save files on public URLs through it. Its pretty light weight.

All you need is a terminal or console.

A Simple GET Rest Call

``` curl https://yourhost/api/getthings

OR

curl -X GEThttps://yourhost/api/getthings


And, you will get the result of this rest endpoint on the console or terminal.

Note: In above example, I assume there is no authentication asked by REST endpoint.

<h2>A POST Call</h2>
Lets assume you have a REST endpoint which takes some data in a POST call.

curl -X POST -H “Content-Type: application/json” -d ’{“msg”: {“to”: “abc-at-smail”}}’ localhost:3000/api/queue/v1


Here, I'm passing a json data to rest endpoint, and mentioned that its a POST call by -X switch.

Note: You need to mention header "Content-Type: application/json" to pass a json to your endpoint. Else, your end point will take it as a string not json.

<h2>Pass Http Headers</h2>
You can pass as many headers to your rest endpoint as you want. Curl provides -H switch, and you pass multiple headers.

curl -H “Host: www.host.com” -H “Authorization: xyz” -H “Content-Type: application/json” localhost:3000/api/queue/v1


<h2>Download static resource like Image</h2>
Assume you have a public url for image like, and you want to download to your machine or server.

curl https://www.owasp.org/images/1/1d/Session-Management-Diagram_Cheat-Sheet.png -o file.png


You specify -o flag to write to a file and give a name. Else, this will display content of that resource on console.

<h2>Download html</h2>
Similarly, you can download html of resource.

curl https://www.owasp.org/index.php/Session_Management_Cheat_Sheet -o file.html


This will download the htmlonly, not any other resources like image or css or javascript.

## Using curl for upload file on AWS S3
See [Uploading to S3 witj Curl](/tutorials/how-upload-aws-s3-curl/)

If you have any difficulty using curl, put it in comment below.

Enjoy
Share

Related Posts

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

How to use Docker for Drupal 7 Dev envirnoment

How to use Docker for Drupal 7 Dev envirnoment

I have been using drupal 7 from a long time, and setting up a dev environment…

Shell script to extract single file from a bunch of jar files

Shell script to extract single file from a bunch of jar files

Each jar file has a manifest file in META_INF/MANIFEST.MF I have to read each…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…